package com.moyun.finance.service;

import com.moyun.finance.entity.OrgDeptUserMid;
import com.moyun.finance.entity.OrgRoleUserMid;
import com.moyun.finance.entity.OrgUser;
import com.moyun.finance.entity.Response;
import com.moyun.finance.repository.OrgDeptUserMidRepository;
import com.moyun.finance.repository.OrgRoleUserMidRepository;
import com.moyun.finance.repository.OrgUserRepository;
import com.moyun.finance.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional
public class OrgUserService {
    @Autowired
    OrgUserRepository our;
    @Autowired
    OrgRoleUserMidService orums;
    @Autowired
    OrgDeptUserMidService odums;
    @Autowired
    OrgRoleUserMidRepository orumr;
    @Autowired
    OrgDeptUserMidRepository odumr;

    public Response save(OrgUser user){
        OrgUser findUser = our.findByUserid(user.getUserid());
        if(findUser != null){
            return new Response("用户名已存在!",false);
        }
        user.setId(BaseUtil.uuid());
        user.setUserPwd(BaseUtil.md5NoSalt(user.getUserPwd(), 3));
        // 删除状态为 未删除
        user.setDelState(0);
        our.save(user);
        saveDept(user);
        saveRole(user);
        return new Response("注册成功！",user,true);
    }

    public Response login(String userid, String userPwd, HttpServletRequest request){
        OrgUser findUser = our.findByUserid(userid);
        setDeptAndRole(findUser);
        String loginPwd = BaseUtil.md5NoSalt(userPwd,3);
        if(findUser == null){
            return new Response("该用户不存在!",false);
        }else if(findUser.getDelState() == 1){
            return new Response("该账号已停用!",false);
        }else if(findUser.getUserPwd().equals(loginPwd)){
            request.getSession().setAttribute("loginUser", findUser);
            return new Response("登录成功!",findUser,true);
        }else{
            return new Response("用户名或密码错误!",false);
        }
    }

    public Response stopUser(String userid,int delState){
        OrgUser findUser = our.findByUserid(userid);
        if(findUser == null){
            return new Response("该用户不存在!",false);
        }
        findUser.setDelState(delState);
        our.save(findUser);
        if(delState == 1){
            return new Response("账号停用成功!",true);
        }else if(delState == 0){
            return new Response("账号启用成功!",true);
        }
        return new Response("操作失败,请联系系统管理员。",false);
    }

//    public Response openUser(String userid){
//        OrgUser findUser = our.findByUserid(userid);
//        if(findUser == null){
//            return new Response("该用户不存在!",false);
//        }
//        findUser.setDelState(0);
//        return new Response("账号启用成功!",true);
//    }


    public Response updateUser(OrgUser user){
        our.save(user);
        saveDept(user);
        saveRole(user);
        return new Response("修改成功!",true);
    }

    public List<OrgUser> findAll(String companyId){
        List<OrgUser> users = our.findByCompanyId(companyId);
        setDeptAndRole(users);
        return users;
    }

    public List<OrgUser> findUserByName(String userName, String companyId){
        List<OrgUser> users = our.findByCompanyIdAndUserNameLike(companyId, "%"+userName+"%");
        setDeptAndRole(users);
        return users;
    }

    public OrgUser findUserById(String userId){
        OrgUser user = our.findById(userId).orElse(null);
        setDeptAndRole(user);
        return user;
    }

    public void setDeptAndRole(OrgUser user){
        user.setDeptName(odums.findUserDeptNameByUser(user.getId()));//把拼接后的部门名放到实体类里
        user.setRoleName(orums.findUserRoleNameByUser(user.getId()));//把拼接后的角色名放到实体类里
        user.setDeptId(odums.findUserDeptIdByUser(user.getId()));
        user.setRoleId(orums.findUserRoleIdByUser(user.getId()));
//        user.setOrgDept(odums.findUserDeptByUser(user.getId()));
//        user.setOrgRole(orums.findUserRoleByUser(user.getId()));
    }

    public void setDeptAndRole(List<OrgUser> users){
        for(OrgUser user : users){
            setDeptAndRole(user);
        }
    }

    public void saveDept(OrgUser user){
        odumr.deleteByUserId(user.getId());//删除已有部门关联信息
        List<String> deptIds = user.getDeptId();
        if(deptIds != null){
            for(String deptId : deptIds){
                OrgDeptUserMid odum = new OrgDeptUserMid();
                odum.setId(BaseUtil.uuid());
                odum.setUserId(user.getId());
                odum.setDeptId(deptId);
                odumr.save(odum);
            }
        }
    }

    public void saveRole(OrgUser user){
        orumr.deleteByUserId(user.getId());//删除已有角色关联信息
        List<String> roleIds = user.getRoleId();
        if(roleIds != null){
            for(String roleId : roleIds){
                OrgRoleUserMid orum = new OrgRoleUserMid();
                orum.setId(BaseUtil.uuid());
                orum.setUserId(user.getId());
                orum.setRoleId(roleId);
                orumr.save(orum);
            }
        }
    }

}
